<template>
  <div class="category">
    <!--显示所有上架的父分类-->
    <ul>
      <li @click="selectFirstCategory({})" :class="{active: !firstCategorySelected.id}">全部</li>
      <li v-for="(category,index) in firstCategoryList" :key=index @click="selectFirstCategory(category)" :class="{active: category.id === firstCategorySelected.id}">

        <RouterLink :to="`/user/search/${category.id}`">{{ category.name }}</RouterLink>
      </li>

    </ul>
    <!--显示父分类下的子分类-->
    <ul v-if="firstCategorySelected.id">
      <li @click="selectSecondCategory({})" :class="{active: !secondCategorySelected.id}">全部</li>
      <li v-for="(category,index) in secondCategoryList" :key=index @click="selectSecondCategory(category)"
          :class="{active: category.id === secondCategorySelected.id}">{{ category.name }}
      </li>
    </ul>
  </div>
  <!--  显示搜索到的商品-->
  <div class="goodsList">
    <ul>
      <li v-for="(goods,index) in pageInfo.list" :key="index" @click="toGoodsView(goods.id)">
        <div class="pic">
          <el-image :src="`${SERVER_ADDR}/goods/pic/${goods.picList[0].url}`"/>
        </div>
        <div class="name">{{ goods.name }}</div>
        <div class="dscp">{{ goods.dscp }}</div>
        <div class="price">{{ goods.price }}</div>
      </li>
    </ul>
    <el-row class="row-bg" justify="center">
      <el-pagination
          background
          layout="prev, pager, next"
          v-model:current-page="pageInfo.pageNum"
          :page-count="pageInfo.pages"
          @update:current-page="search"
      >
      </el-pagination>
    </el-row>
  </div>
</template>

<script setup>
import {ref, watch} from "vue";
import categoryApi from "@/api/categoryApi.js";
import goodsApi from "@/api/goodsApi.js";
import {useRouter} from "vue-router";

const SERVER_ADDR = ref(import.meta.env.VITE_SERVER_ADDR);
const router = useRouter()
import {useRoute} from "vue-router";

const route = useRoute()
//通过路径传递的参数
const props = defineProps({
  categoryId: String,
  keyword: String
})
//通过路径传递的参数发生变化进行的处理
watch(props, () => {
  getParent()
})
//所有需要显示的父分类 上架 推荐
const firstCategoryList = ref([])
//所有需要显示的父分类
const secondCategoryList = ref([])
//被选中的父分类
const firstCategorySelected = ref({})
//被选中的子分类
const secondCategorySelected = ref({})
//被选中的分类id，可能是父分类id也可能是子分类id
const categoryId = ref(0)
//在路径上传递的name
const name = ref(props.name)
//分页信息
const pageInfo = ref({
  pages: 0,
  pageSize: 0,
  pageNum: 0
})

//点击父分类调用的函数
function selectFirstCategory(category) {
  //给选中的父分类赋值
  firstCategorySelected.value = category;
  //只要选择某个父分类，就要清空所有要显示的子分类
  secondCategoryList.value = []
  //给被选中的分类id赋值
  if (firstCategorySelected.value.id) {
    categoryId.value = firstCategorySelected.value.id
  } else {
    categoryId.value = null
  }

//给选中的父分类下的子分类赋值
  if (category.childList) {
    category.childList
        .filter(c => c.status === 1)
        .forEach(c => secondCategoryList.value.push(c))
  }
  search(1)
}

//点击子分类调用的函数
function selectSecondCategory(category) {
  //给选中的子分类赋值
  secondCategorySelected.value = category;
  //给被选中的分类id赋值
  if (secondCategorySelected.value.id) {
    categoryId.value = secondCategorySelected.value.id
  } else {
    categoryId.value = firstCategorySelected.value.id
  }
  search(1)
}

//1.获取父分类--2.上架--3.推荐
function getParent() {
  const condition = {
    parentId: 0,
    recom: 1,
    status: 1,
  }
  categoryApi.selectByPage(condition)
      .then(resp => {
        console.log(resp.data)
        firstCategoryList.value = resp.data;
        //假设没有通过路径传递参数，或者传递的id在父分类中不存在
        let flag = true
        firstCategoryList.value.forEach(c => {
          if (props.categoryId === c.id) {
            flag = false
            selectFirstCategory(c)
          }
        })
        //假设成立
        if (flag === true) {
          //父分类就选择全部
          selectFirstCategory({})
        }
      })
}

//搜索
function search(pageNum) {
  const condition = {
    status: 0,
    categoryId: categoryId.value,
    name: props.keyword,
  };
  goodsApi.selectByPage1(condition, pageNum, 20)
      .then(resp => {
        pageInfo.value = resp.data
        console.log(resp.data)
      })
}

function toGoodsView(id) {
  router.push({
    path: '/user/goods',//跳转到的位置，值和页面路由中配置的路径相同
    query: {
      id: id
    }
  })
}

getParent()
</script>

<style scoped>
.category ul li {
  float: left;
  margin: 10px 20px;
  font-size: 15px;
  cursor: pointer;
}

.active {
  color: var(--theme-color);
  font-weight: bold;
}

.goodsList ul li {
  width: 224px;
  float: left;
  margin-right: 20px;
  margin-bottom: 20px;
  text-align: center;
  background-color: #eee;
  cursor: pointer;
}

.goodsList ul li:hover {
  box-shadow: 0 0 20px #ff6800;
}

.goodsList ul li:nth-child(5n) {
  margin-right: 0;
}

.name .desp .price {
  line-height: 25px;
}

.name {
  font-size: 16px;
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>